package com.hao.mydownloadlist;

import android.content.Context;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.hao.mydownloadlist.event.DownloadEvent;
import com.hao.mydownloadlist.event.DownloadStateEvent;
import com.hao.mydownloadlist.service.VersionUpdateService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    Button start;
    ListView list;
    List<DownloadEvent> downList;
    MyAdapter myAdapter;

    private String urls[] = {
            "http://s0.youdaoli.fm/f/graphics/20161117/582d0ca70f0f7.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161117/582d0cd111160.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161117/582d0cfa8852e.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161118/582e7886b9b3d.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161121/5832599ae1046.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161121/583259da469f1.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161108/58219a9783446.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161108/58219ae8d2a99.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161121/583280a3be531.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161114/58290de908207.mp3",
    };

    int testPosition = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        start = (Button) findViewById(R.id.start);
        list = (ListView) findViewById(R.id.list);

        downList = new ArrayList<>();
        myAdapter = new MyAdapter();
        myAdapter.setContext(this);
        myAdapter.setListitem(downList);
        list.setAdapter(myAdapter);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (testPosition == urls.length - 1) {
                    return;
                } else {
                    DownloadEvent downloadEvent = creatDownloadBean("kankan_" + testPosition, urls[testPosition]);
                    downList.add(downloadEvent);
                    myAdapter.setListitem(downList);
                    myAdapter.notifyDataSetChanged();
                    VersionUpdateService.beginUpdate(MainActivity.this, MainActivity.class,downloadEvent);
                }
                testPosition++;
            }
        });

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

    }

    DownloadEvent creatDownloadBean(String content, String url) {
        String type[] = url.split("\\.");
        Log.i(TAG,">>>>>>>" + type.length);
        return new DownloadEvent().setType(type[type.length - 1]).setName(content).setUrl(url).setIconId(R.mipmap.ic_launcher);
    }


    class MyAdapter extends BaseAdapter {


        List<DownloadEvent> listitem;

        Context context;


        ViewHolder mViewHolder;


        public void setContext(Context context) {
            this.context = context;
        }

        public void setListitem(List<DownloadEvent> listitem) {
            this.listitem = listitem;
        }

        @Override
        public Object getItem(int position) {
            return listitem != null ? listitem.get(position) : null;
        }

        @Override
        public int getCount() {
            return listitem == null ? 0 : listitem.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewholder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
                viewholder = new ViewHolder();
                viewholder.init(convertView);
                convertView.setTag(viewholder);
            } else {
                viewholder = (ViewHolder) convertView.getTag();
            }
            DownloadEvent downloadBean = listitem.get(position);
            if (downloadBean.getStatus() == 0) {
                viewholder.content.setText("等待中。。。");
            } else if (downloadBean.getStatus() == -1) {
                mViewHolder = viewholder;
                viewholder.content.setText(downloadBean.getCurrent() + "%");
            } else if (downloadBean.getStatus() == 1) {
                viewholder.content.setText("已完成");
            } else {
                viewholder.content.setText("出现问题");
            }
            viewholder.name.setText(downloadBean.getName());
            return convertView;
        }

        class ViewHolder {

            TextView name;
            TextView content;

            void init(View view) {
                name = (TextView) view.findViewById(R.id.name);
                content = (TextView) view.findViewById(R.id.coutent);
            }
        }

        public ViewHolder getViewHolder() {
            return mViewHolder;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DownloadStateEvent event) {
        downList.get(event.getPosition()).setStatus(event.getState());
        downList.get(event.getPosition()).setCurrent(event.getCurrent());
        myAdapter.setListitem(downList);
        myAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            EventBus.getDefault().unregister(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(this,VersionUpdateService.class);
        stopService(intent);
    }
}
